package net.hubt.itc.data;

import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ClassLookXMLParser extends BasicXMLParser {
	
	static final String KEY_DS_LOP = "Row";
	static final String KEY_KHOA_HOC = "KHOA";
	static final String KEY_NGANH_HOC = "NGANH";// parent node
	static final String KEY_LOP_HOC = "LOP";
	
	private Element mRootNode;

	public void setRoot(Element root) {
		mRootNode = root;		
	}

	public String getKhoaHoc() {
		return getTagValueLevel1(KEY_KHOA_HOC, mRootNode);
	}

	public String getNganhHoc() {
		return getTagValueLevel1(KEY_NGANH_HOC, mRootNode);
	}

	public String getLopHoc() {
		return getTagValueLevel1(KEY_LOP_HOC, mRootNode);
	}

	public List<ClassLook> parseXmlData() {
		List<ClassLook> classlookDataList = null;
		if (mRootNode != null) {
			if (mRootNode.getChildNodes().getLength() > 0) {
				// only one student name with a list of transcript data
				classlookDataList = new ArrayList<ClassLook>();

				NodeList nlDSLop = mRootNode
						.getElementsByTagName(KEY_DS_LOP);
				if (nlDSLop.getLength() > 0) {
					// found <MONHOC>
					int cntDSLop = nlDSLop.getLength();
					// extract every single <LICHTHI> element tag
					for (int i = 0; i < cntDSLop; i++) {
						// a single <LICHTHI> element tag
						Node nodeLop = nlDSLop.item(i);
						if (nodeLop.getNodeType() == Node.ELEMENT_NODE) {
							Element element = (Element) nodeLop;

							ClassLook classlookDataItem = new ClassLook();
							
							classlookDataItem.setNganh(getTagValue(
									KEY_NGANH_HOC, element));
							classlookDataItem.setTenLop(getTagValue(
									KEY_LOP_HOC, element));
							classlookDataItem.setKhoaHoc(getTagValue(
									KEY_KHOA_HOC, element));
							classlookDataItem.setCheck(false);
	
							classlookDataList.add((ClassLook)classlookDataItem.clone());
						}
					}
				}
			}
		}
		return classlookDataList;
	}
}
